const fs = require('fs')
const { exec } = require('child_process')
const { logCyan, logRed } = require('./utils')

function cloneAll(clonePath, urlArr) {
	return new Promise((resolve, reject) => {
		if (!fs.existsSync(clonePath)) {
			return logRed('找不到指定路径')
		}
		let process = ''
		let downloadable = new Array()
		for (const url of urlArr) {
			process = exec(`cd ${clonePath} && git clone ${url}`, {})
			process.stderr.on('data', (data) => {
				logCyan(data)
			})
			process.on('close', (code) => {
				if (code !== 0) {
					logRed(`git clone 进程退出，退出码 ${code}`)
				}
				if (code === 0) {
					downloadable.push(url)
				}
				if (downloadable.length === urlArr.length) {
					resolve(`process close ==> ${url}`)
				}
			})
		}
	})
}
module.exports = {
	cloneAll
}
